package com.yh.workbill.ui.companyInfo;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.yh.workbill.App;
import com.yh.workbill.R;
import com.yh.workbill.adapter.CompanyListInfoAdapter;
import com.yh.workbill.base.BaseActivity;
import com.yh.workbill.databinding.ActivityCompanyListInfoBinding;
import com.yh.workbill.databinding.LayoutHeadBinding;
import com.yh.workbill.db.CompanyDao;
import com.yh.workbill.model.Company;
import com.yh.workbill.ui.companyInfo.mvp.AddCompanyInfo_Presenter;
import com.yh.workbill.ui.companyInfo.mvp.AddCompanyInfo_View;
import com.yh.workbill.utils.Utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.arch.core.executor.ArchTaskExecutor;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;

public class CompanyListInfoActivity extends BaseActivity implements AddCompanyInfo_View {

    private ActivityCompanyListInfoBinding binding;
    private LayoutHeadBinding layoutHead;
    private CompanyListInfoViewModel viewModel;
    private List<Company> listData = new ArrayList<>();
    private LiveData<List<Company>> liveData;
    private CompanyListInfoAdapter adapter;
    private AddCompanyInfo_Presenter presenter;
    private int pageindex = 1, pagenumber = 20;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_company_list_info);
        layoutHead = binding.layoutHead;
    }

    @Override
    public void initJetPack(@Nullable Bundle savedInstanceState) {
        viewModel = ViewModelProviders.of(this).get(CompanyListInfoViewModel.class);
        presenter = new AddCompanyInfo_Presenter(this);
        this.getLifecycle().addObserver(presenter);
        binding.setLifecycleOwner(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        pageindex = 1;
        presenter.getQueryData(viewModel, true, false, pageindex, pagenumber);
        liveData = viewModel.getCompanydatas();
        liveData.observe(this, companies -> {
            for (Company b : companies) {
                Log.e("TAG", "信息是:" + b.company_name + " " + b.company_phone + " " + b.company_address + "\n");
            }
            adapter.setData(companies);
        });
    }

    @Override
    public void initObserve(@Nullable Bundle savedInstanceState) {
        adapter = new CompanyListInfoAdapter(listData);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        binding.recyclerView.setAdapter(adapter);

        adapter.setOnLongClickLister(new CompanyListInfoAdapter.OnLongClickLister() {
            @Override
            public void onItemClick(int position, Company data) {
                new AlertDialog.Builder(CompanyListInfoActivity.this).setTitle("删除")
                        .setMessage("是否删除 " + data.getCompany_name() + " ?")
                        .setPositiveButton("删除", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                presenter.delCompany(data.company_name, data.uuid);
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .create()
                        .show();
            }
        });
    }

    @Override
    public void initBaseSetting(@Nullable Bundle savedInstanceState) {
        layoutHead.setTitle("公司信息");

    }

    @Override
    public void initClick(@Nullable Bundle savedInstanceState) {
        layoutHead.llBlack.setOnClickListener(v -> finish());
        binding.btnAddCompany.setOnClickListener(v -> {
            Intent i = new Intent(this, AddCompanyInfoActivity.class);
            startActivity(i);
        });

        binding.refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageindex = 1;
                presenter.getQueryData(viewModel, true, false, pageindex, pagenumber);
            }
        });

        binding.refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                presenter.getQueryData(viewModel, false, true, pageindex, pagenumber);
            }
        });
    }

    @Override
    public void initDefaultMethod(@Nullable Bundle savedInstanceState) {

    }

    @Override
    public void getData(List<com.yh.workbill.model.Company> data) {
        pageindex++;
    }

    @Override
    public void addSuccess(String company, String company_address, String company_phone) {

    }

    @Override
    public void addFail(String msg) {
        Utils.ShowToas(this, msg);
    }

    @Override
    public void queryFail(String msg) {
        Utils.ShowToas(this, msg);
        binding.refreshLayout.finishRefresh();
        binding.refreshLayout.finishLoadMore();
    }

    @Override
    public void delSuccess(String company_name) {
        Utils.ShowToas(this, "删除成功");
        ArchTaskExecutor.getIOThreadExecutor().execute(() -> {
            CompanyDao dao = App.appDataBase.getCompanyDao();
            dao.delectName(company_name);
        });
        List<Company> value = viewModel.getCompanydatas().getValue();
        Iterator<Company> it = value.iterator();
        while (it.hasNext()) {
            Company next = it.next();
            if (next.getCompany_name().equals(company_name)) {
                it.remove();
            }
        }
        viewModel.update();
    }

    @Override
    public void refresh() {
        binding.refreshLayout.finishRefresh();
    }

    @Override
    public void loadMore() {
        binding.refreshLayout.finishLoadMore();
    }

    @Override
    public void getDataFail() {
        binding.refreshLayout.finishRefresh();
        binding.refreshLayout.finishLoadMore();
    }

    @Override
    public void showTasot(String message) {
        Utils.ShowToas(this, message);
    }

    @Override
    public void showDialog(String content, boolean isCancelable) {
        showLoaddingDialog(content, isCancelable);
    }

    @Override
    public void hideDialog() {
        hideLoaddingDialog();
    }
}
